package top.yongyan0624.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.ServletRequestUtils;
import org.springframework.web.multipart.MultipartFile;
import top.yongyan0624.common.lang.Const;
import top.yongyan0624.service.*;
import top.yongyan0624.utils.RedisUtils;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author Yongyan
 * @description: TODO
 * @date 2021/8/15
 **/
public class BaseController {
    @Autowired
    HttpServletRequest req;

    @Autowired
    RedisUtils redisUtils;

    @Autowired
    SysUserService sysUserService;

    @Autowired
    SysRoleService sysRoleService;

    @Autowired
    SysMenuService sysMenuService;

    @Autowired
    SysUserRoleService sysUserRoleService;

    @Autowired
    SysRoleMenuService sysRoleMenuService;

    @Autowired
    VoteService voteService;

    @Autowired
    VoteUserService voteUserService;

    @Autowired
    CommunityService communityService;

    @Autowired
    ItemFindService itemFindService;

    @Autowired
    ItemLostService itemLostService;

    @Autowired
    GoodsService goodsService;

    @Autowired
    GoodsGroupService goodsGroupService;

    @Autowired
    AlipayService alipayService;

    @Autowired
    AlipayGroupService alipayGroupService;

    @Autowired
    ItemBorrowService itemBorrowService;

    @Autowired
    ItemBorrowUserService itemBorrowUserService;

    @Autowired
    StoreCartService storeCartService;

    @Autowired
    StoreCartGroupService storeCartGroupService;

    @Autowired
    PropertyPayService propertyPayService;

    @Autowired
    PropertyPayProjectService propertyPayProjectService;

    @Autowired
    PropertyRepairService propertyRepairService;

    @Autowired
    ShopService shopService;

    @Autowired
    PropertyComplaintService propertyComplaintService;

    @Autowired
    DynamicService dynamicService;

    @Autowired
    DynamicUserService dynamicUserService;

    @Autowired
    DynamicCommentService dynamicCommentService;

    @Autowired
    NoticeService noticeService;

    /**
     * 获取页面
     * @return page对象
     */
    public Page getPage() {
        int current = ServletRequestUtils.getIntParameter(req, "current", 1);
        int size = ServletRequestUtils.getIntParameter(req, "size", 10);

        return new Page(current, size);
    }

    public String handleFile(String dirName, Long id, MultipartFile[] files) throws IOException {
        //存储所有文件的虚拟路径名
        List<String> resultStringList = new ArrayList<>();

        Arrays.stream(files).forEach(file -> {
            // 获取文件名
            String fileName = file.getOriginalFilename();
            // 获取文件的后缀名
            String suffixName = fileName.substring(fileName.lastIndexOf("."));
            //生成新的文件名
            fileName = dirName + "/" + id + "/" + System.currentTimeMillis() + suffixName;
            resultStringList.add(Const.FILE_VIRTUAL_PATH + fileName);
            // 设置文件存储路径
            File dest = new File(new File(Const.FILE_REALLY_PATH + fileName).getAbsolutePath());// dist为文件，有多级目录的文件
            // 检测是否存在目录
            if (!dest.getParentFile().exists()) {//因此这里使用.getParentFile()，目的就是取文件前面目录的路径
                dest.getParentFile().mkdirs();// 新建文件夹
            }

            try {
                file.transferTo(dest);// 文件写入
            } catch (IOException e) {
                e.printStackTrace();
            }
        });

        //返回虚拟路径名
        return StrUtil.join(",", resultStringList);
    }

    public String handleFile(String dirName, Long id, MultipartFile file) throws IOException {
            // 获取文件名
            String fileName = file.getOriginalFilename();
            // 获取文件的后缀名
            String suffixName = fileName.substring(fileName.lastIndexOf("."));
            //生成新的文件名
            fileName = dirName + "/" + id + "/" + System.currentTimeMillis() + suffixName;
            // 设置文件存储路径
            File dest = new File(new File(Const.FILE_REALLY_PATH + fileName).getAbsolutePath());// dist为文件，有多级目录的文件
            // 检测是否存在目录
            if (!dest.getParentFile().exists()) {//因此这里使用.getParentFile()，目的就是取文件前面目录的路径
                dest.getParentFile().mkdirs();// 新建文件夹
            }

            file.transferTo(dest);// 文件写入

        //返回虚拟路径名
        return Const.FILE_VIRTUAL_PATH + fileName;
    }
}
